﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPEasyCode
{
    public class TemplateIO
    {
        /// <summary>
        /// Reads specific line in given template  
        /// </summary>
        /// <param name="lineNumber">Line number </param>
        /// <param name="filePath">File path </param>
        /// <returns></returns>
        public static string ReadNextLine(int lineNumber, string filePath)
        {
            int counter = 0;
            string line;

            using (System.IO.StreamReader file =
              new System.IO.StreamReader(filePath))
            {
                while ((line = file.ReadLine()) != null)
                {
                    if (counter == lineNumber)
                        break;
                    counter++;
                }
            }

            return line;
        }

        /// <summary>
        /// Returns total line count of given template
        /// </summary>
        /// <param name="filePath">file path</param>
        /// <returns>line count</returns>
        public static int LineCount(string filePath)
        {
            int counter = 0;
            string line;
            bool fileExsists = System.IO.File.Exists(filePath);
            if (fileExsists)
            {
                using (System.IO.StreamReader file =
                  new System.IO.StreamReader(filePath))
                {
                    while ((line = file.ReadLine()) != null)
                    {

                        counter++;
                    }
                }
            }

            return counter;
        }

        /// <summary>
        /// write text to given template
        /// </summary>
        /// <param name="text">text</param>
        /// <param name="path">file path</param>
        public static void Write(string text, string path)
        {
            byte[] byteArray = Encoding.ASCII.GetBytes(text);
            string line = string.Empty;
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path ))
            {
                using (System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray))
                {
                    using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
                    {

                        while ((line = reader.ReadLine()) != null)
                        {

                            file.WriteLine(line);
                        }
                    }
                }
            }
        }
        
        /// <summary>
        /// Write file after specific line  
        /// </summary>
        /// <param name="text">Text</param>
        /// <param name="path">Template path</param>
        /// <param name="lineCount">Line count</param>
        public static void WriteFile(string text, string path, int lineCount)
        {
            int count = lineCount;
            int totCount = GetLineCount(text);
            bool copyright = true;
            //Users are not allowed to remove this text.
            string copyrightText = @"//This file has been automaticaly generated by SPAC 2012";
            string stratLine =     @"//*******************************************************";
            string currentLine = GetLineFromText(text, count);
            if (LineProcesser.Save(currentLine))
            {
                GlobalData.ClassName = LineProcesser.GetClassFileName(currentLine);
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(path + "\\" + GlobalData.ClassName))
                {

                    while (!LineProcesser.SaveEnd(currentLine))
                    {
                        if (copyright)
                        {
                            file.WriteLine(stratLine);
                            file.WriteLine(copyrightText);
                            file.WriteLine(stratLine);
                            copyright = false;
                        }
                        count++;
                        currentLine = GetLineFromText(text, count);

                        if (!LineProcesser.SaveEnd(currentLine) && !LineProcesser.SaveStart(currentLine))
                            file.WriteLine(currentLine);
                    }
                }
            }

            if (totCount-1 != count)
            {
                WriteFile(text, path, count + 1);
            }
        }


        /// <summary>
        /// Retrieve specific line form template 
        /// </summary>
        /// <param name="text">Text</param>
        /// <param name="lineNumber">line number</param>
        /// <returns>Line</returns>
        private static string GetLineFromText(string text, int lineNumber)
        {

            byte[] byteArray = Encoding.ASCII.GetBytes(text);
            int counter = 0;
            string line = string.Empty;
            using (System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray))
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
                {

                    while ((line = reader.ReadLine()) != null)
                    {
                        if (counter == lineNumber)
                            break;
                        counter++;
                    }
                }
            }

            return line;
        }


        /// <summary>
        /// Get line count of text 
        /// </summary>
        /// <param name="text">text</param>
        /// <returns>line count</returns>
        private static int GetLineCount(string text)
        {

            byte[] byteArray = Encoding.ASCII.GetBytes(text);
            int counter = 0;
            string line = string.Empty;
            using (System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray))
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
                {

                    while ((line = reader.ReadLine()) != null)
                    {

                        counter++;
                    }
                }
            }

            return counter;
        }
    }
}
